package com.llyb.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.llyb.lease.model.entity.FeeKey;
import com.llyb.lease.model.entity.FeeValue;
import com.llyb.lease.web.admin.mapper.FeeKeyMapper;
import com.llyb.lease.web.admin.mapper.FeeValueMapper;
import com.llyb.lease.web.admin.service.FeeKeyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.llyb.lease.web.admin.service.FeeValueService;
import com.llyb.lease.web.admin.vo.fee.FeeKeyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author liubo
* @description 针对表【fee_key(杂项费用名称表)】的数据库操作Service实现
* @createDate 2023-07-24 15:48:00
*/
@Service
public class FeeKeyServiceImpl extends ServiceImpl<FeeKeyMapper, FeeKey>
    implements FeeKeyService{

    @Autowired
    private FeeKeyMapper feeKeyMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Override
    public List<FeeKeyVo> customFindAll() {
        QueryWrapper<FeeKey> wrapper = new QueryWrapper<>();
        wrapper.select("id","name");
        List<FeeKey> feeKeyList = feeKeyMapper.selectList(wrapper);
        List<FeeValue> feeValueList = feeValueMapper.selectList(null);

        ArrayList<FeeKeyVo> resultList = new ArrayList<>();
        for (FeeKey feeKey : feeKeyList) {
            FeeKeyVo feeKeyVo = new FeeKeyVo();

            BeanUtils.copyProperties(feeKey,feeKeyVo);
            ArrayList<FeeValue> list = new ArrayList<>();
            for (FeeValue feeValue : feeValueList) {
                if (feeKey.getId().equals(feeValue.getFeeKeyId())) {
                    list.add(feeValue);
                }
            }
            feeKeyVo.setFeeValueList(list);
            resultList.add(feeKeyVo);
        }

        return resultList;
    }

    @Override
    public void customRemoveByFeeKeyId(Long feeKeyId) {
        LambdaQueryWrapper<FeeValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FeeValue::getFeeKeyId,feeKeyId);
        feeValueMapper.delete(lambdaQueryWrapper);

        LambdaQueryWrapper<FeeKey> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(FeeKey::getId,feeKeyId);
        feeKeyMapper.delete(lambdaQueryWrapper1);
    }
}




